/**
 * Created by JasonChou on 16/4/28.
 */
import Vue from 'vue';
import VueRouter from 'vue-router';
Vue.use(VueRouter);

export var router = new VueRouter();

router.map({

    home: {
        name: 'home',
        component: function (resolve) {
            require.ensure(['./components/home.vue'], function (require) {
                var component = require('./components/home.vue');
                resolve(component);
            }, 'home');
        }
    },

    cate: {
        name: 'cate',
        component: function (resolve) {
            require.ensure(['./components/cate.vue'], function (require) {
                var component = require('./components/cate.vue');
                resolve(component);
            }, 'cate')
        }
    },
    'product/:spu_id/:sku_id': {
        name: 'product',
        component: function (resolve) {

            require.ensure(['./components/product.vue'], function (require) {
                var component = require('./components/product.vue');
                resolve(component);
            }, 'product')
        }
    },
    'order/confirm/:skus': {
        name: 'confirmOrder',
        component: function (resolve) {
            require.ensure(['./components/confirmOrder.vue'], function (require) {
                var component = require('./components/confirmOrder.vue');
                resolve(component);
            }, 'confirmOrder')
        }
    },
    'addresses/add': {
        name: 'addAddress',
        component: function (resolve) {
            require.ensure(['./components/addAddress.vue'], function (require) {
                var component = require('./components/addAddress.vue');
                resolve(component);
            }, 'addAddress')
        }
    },
    'addresses/edit/:address': {
        name: 'editAddress',
        component: function (resolve) {
            require.ensure(['./components/addAddress.vue'], function (require) {
                var component = require('./components/addAddress.vue');
                resolve(component);
            }, 'addAddress')
        }
    },
    'addresses': {
        name: 'addresses',
        component: function (resolve) {
            require.ensure(['./components/addresses.vue'], function (require) {
                var component = require('./components/addresses.vue');
                resolve(component);
            }, 'addresses')
        }
    },
    'addresses/select': {
        name: 'selectAddress',
        component: function (resolve) {
            require.ensure(['./components/addresses.vue'], function (require) {
                var component = require('./components/addresses.vue');
                resolve(component);
            }, 'addresses')
        }
    },
    'products/:category/:category_name/:brand/:brand_name': {
        name: 'products',
        component: function (resolve) {
            require.ensure(['./components/products.vue'], function (require) {
                var component = require('./components/products.vue');
                resolve(component);
            }, 'products')
        }
    },

    'products/:spu/:spu_name': {
        name: 'productsBySpu',
        component: function (resolve) {
            require.ensure(['./components/products.vue'], function (require) {
                var component = require('./components/products.vue');
                resolve(component);
            }, 'products')
        }
    },

    seckills: {
        name: 'seckills',
        component: function (resolve) {
            require.ensure(['./components/seckills.vue'], function (require) {
                var component = require('./components/seckills.vue');
                resolve(component);
            }, 'seckills')
        }
    }, 404: {
        name: '404',
        component: function (resolve) {
            require.ensure(['./components/404.vue'], function (require) {
                var component = require('./components/404.vue');
                resolve(component);
            }, '404')
        }
    },
    mine: {
        name: 'mine',
        component: function (resolve) {
            require.ensure(['./components/mine.vue'], function (require) {
                var component = require('./components/mine.vue');
                resolve(component);
            }, 'mine')
        }
    },
    orders: {
        name: 'orders',
        component: function (resolve) {
            require.ensure(['./components/orders.vue'], function (require) {
                var component = require('./components/orders.vue');
                resolve(component);
            }, 'orders')
        }
    },
    'order/:order_id': {
        name: 'order',
        component: function (resolve) {
            require.ensure(['./components/order.vue'], function (require) {
                var component = require('./components/order.vue');
                resolve(component);
            }, 'order')
        }
    },

    cart: {
        name: 'cart',
        component: function (resolve) {
            require.ensure(['./components/cart.vue'], function (require) {
                var component = require('./components/cart.vue');
                resolve(component);
            }, 'cart')
        }
    },

    coupons: {
        name: 'coupons',
        component: function (resolve) {
            require.ensure(['./components/coupons.vue'], function (require) {
                var component = require('./components/coupons.vue');
                resolve(component);
            }, 'coupons')
        }
    },

    'coupons/select': {
        name: 'selectCoupon',
        component: function (resolve) {
            require.ensure(['./components/coupons.vue'], function (require) {
                var component = require('./components/coupons.vue');
                resolve(component);
            }, 'coupons')
        }
    },

    service: {
        name: 'service',
        component: require('./components/service.vue')
    }
});

router.redirect({
    '/': 'home'
});


router.redirect({
    '*': '/404'
});

router.beforeEach(function (transition) {
    if (window.scrollY > 0)
        positionCache[transition.from.path] = window.scrollY;


    if (refreshCache[transition.to.name]) {
        transition.to.params.refresh = true;
        delete  refreshCache[transition.to.name];
    }

    console.log(transition);

    transition.next();
});


